//
//  ViewController.m
//  图片移动
//
//  Created by 小果 on 16/7/2.
//  Copyright © 2016年 小果. All rights reserved.
//
/**
 *******************************************************
 type:动画类型(比如：滴水效果，翻转效果...)
 -------------------------------------------------------
 fade kCATransitionFade 交叉淡化过渡
 moveIn kCATransitionMoveIn 新视图移到旧视图上面
 push kCATransitionPush 新视图把旧视图推出去
 reveal kCATransitionReveal 将旧视图移开,显示下面的新视图
 pageCurl               向上翻一页
 pageUnCurl             向下翻一页
 rippleEffect             滴水效果
 suckEffect 收缩效果，如一块布被抽走
 cube                   立方体效果
 oglFlip              上下左右翻转效果
 rotate     旋转效果
 cameraIrisHollowClose 相机镜头关上效果(不支持过渡方向)
 cameraIrisHollowOpen 相机镜头打开效果(不支持过渡方向)
 
 *******************************************************
 subtype: 动画方向(比如说是从左边进入，还是从右边进入...)
 ------------------------------------------------------
 kCATransitionFromRight;
 kCATransitionFromLeft;
 kCATransitionFromTop;
 kCATransitionFromBottom;
 
 当 type 为@"rotate"(旋转)的时候,它也有几个对应的 subtype,分别为:
 90cw 逆时针旋转 90°
 90ccw 顺时针旋转 90°
 180cw 逆时针旋转 180°
 180ccw  顺时针旋转 180°
 **/

#import "ViewController.h"
#import "ImageCell.h"
#import "CellModel.h"
#import "UIView+Common.h"
#import "TestViewController.h"
@interface ViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic, strong) NSArray *imgArray;

@property (nonatomic, strong) UICollectionView *collectionView;

@end

@implementation ViewController
#pragma mark - 懒加载
-(NSArray *)imgArray{
    if (_imgArray == nil) {
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"image.plist" withExtension:nil];
        _imgArray = [NSArray arrayWithContentsOfURL:url];
        
        NSMutableArray *mtArray = [NSMutableArray array];
        for (NSDictionary *dict in _imgArray) {
            CellModel *model = [CellModel cellModelWithDict:dict];
            
            [mtArray addObject:model];
        }
        _imgArray = mtArray;
    }
    return _imgArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 创建标题
    [self setUpTitle];
    
    // 设置导航左右item
    [self setUpNavigationBarItem];
    
    // 创建CollectionView
    [self createCollectionView];
    
}
#pragma mark - 创建标题
-(void)setUpTitle{
    UIButton *titleBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 20, 10)];
    [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    titleBtn.titleLabel.font = [UIFont boldSystemFontOfSize:20];
    [titleBtn setTitle:@"cell中图片的移动" forState:UIControlStateNormal];
    [titleBtn addTarget:self action:@selector(titleClickAnimation) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.titleView = titleBtn;
}
#pragma mark - 标题动画
-(void)titleClickAnimation{
    CATransition *anim = [CATransition animation];
    anim.type = @"rotate";
    //    // subtype 不一定每个转场动画的都要
        anim.subtype = @"90ccw";
//    anim.type = @"push";
    anim.repeatCount = 2;
    anim.duration = 2.0;
    [self.collectionView.layer addAnimation:anim forKey:nil];
}
#pragma mark - 设置导航左右item
-(void)setUpNavigationBarItem{
    
    UIButton *rbtn = [UIButton buttonWithType:UIButtonTypeContactAdd];
    [rbtn addTarget:self action:@selector(startsAnimation) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rbtn];
    
    UIButton *lbtn = [UIButton buttonWithType:UIButtonTypeInfoDark];
    [lbtn addTarget:self action:@selector(startAnimation) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:lbtn];
}
#pragma mark - 左边按钮动画
-(void)startAnimation{
    
    CATransition *anim = [CATransition animation];
    //    anim.type = @"rotate";
    //    // subtype 不一定每个转场动画的都要
    //    anim.subtype = @"90ccw";
    
    //    anim.type = @"rippleEffect";
    anim.type = @"suckEffect";
    anim.repeatCount = 2;
    anim.duration = 1.5;
    [self.collectionView.layer addAnimation:anim forKey:nil];
    
}
#pragma mark - 右边按钮动画
-(void)startsAnimation{
    
    CATransition *anim = [CATransition animation];
    //    anim.type = @"rotate";
    //    // subtype 不一定每个转场动画的都要
    //    anim.subtype = @"90ccw";
    
    anim.type = @"rippleEffect";
    //    anim.type = @"cube";
    anim.repeatCount = 2;
    anim.duration = 2.0;
    [self.collectionView.layer addAnimation:anim forKey:nil];
    
}
#pragma mark - 跳转控制器动画
-(void)cellClickAnimation{
    CATransition *anim = [CATransition animation];
    anim.type = @"oglFlip";
    anim.repeatCount = 2;
    anim.duration = 2.0;
    [self.collectionView.layer addAnimation:anim forKey:nil];

}

#pragma mark - 创建CollectionView
-(void)createCollectionView{
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:[self creatLayout]];
    _collectionView.backgroundColor = [UIColor greenColor];
    CGRect frame = self.view.bounds;

    _collectionView.frame = frame;
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    [_collectionView registerClass:[ImageCell class] forCellWithReuseIdentifier:@"fire"];
    
    [self.view addSubview:_collectionView];
    
}
#pragma mark - 创建自动布局
-(UICollectionViewLayout *)creatLayout{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    layout.itemSize = CGSizeMake(screenWidth() - 20, 130);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    
    return layout;
    
}
#pragma mark - 数据源方法
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
        return self.imgArray.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ImageCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"fire" forIndexPath:indexPath];
    CGFloat yOffset = ((_collectionView.contentOffset.y - cell.frame.origin.y) / IMAGE_HEIGHT) * IMAGE_OFFSET_SPEED;
    
    cell.imgOffset = CGPointMake(0.0f, yOffset);
    cell.model = self.imgArray[indexPath.item];
    
    return cell;
}
#pragma mark - 代理方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CellModel *model = self.imgArray[indexPath.item];
    
    TestViewController *test = [[TestViewController alloc] init];
    test.img = [UIImage imageNamed:model.icon];
    test.st = model.name;
    [UIView animateWithDuration:3 delay:2.0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [self cellClickAnimation];
    } completion:^(BOOL finished) {
        [self.navigationController pushViewController:test animated:YES];
    }];
    
}
#pragma mark - 当试图滚动的时候执行的方法
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    for (ImageCell *view in self.collectionView.visibleCells) {
        CGFloat yOffset = ((_collectionView.contentOffset.y - view.frame.origin.y) / IMAGE_HEIGHT) * IMAGE_OFFSET_SPEED;
        view.imgOffset = CGPointMake(0.0f, yOffset);
    }
}


@end
